/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.tkgarment.system.action;

import com.tkgarment.system.dao.HistoryDAO;
import com.tkgarment.system.domain.History;
import com.tkgarment.system.factory.DAOFactory;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;

/**
 *
 * @author zraj
 */
public class EmpHistoryAction extends ActionSupportBase implements SessionAware{

     private Map<String, Object> session;
    List<History> history;
    List<History> gridModel;
    public String execute() {
        try {
           String empId = session.get("session_emp_id").toString();
           HistoryDAO dao = DAOFactory.createHistoryDao();
           setHistory(dao.getByEmpID(empId));
           
            setRecords(history.size());

        if (getTotalrows() != null) {
            setRecords(getTotalrows());
        }

        // Calucalate until rows ware selected
        int to = (getRows() * getPage());

        // Calculate the first row to read
        int from = to - getRows();

        // Set to = max rows
        if (to > getRecords()) {
            to = getRecords();
        }

        if (isLoadonce()) {
            if (getTotalrows() != null && getTotalrows() > 0) {
                setGridModel(history.subList(0, getTotalrows()));
            } else {
                // All Custumer
                setGridModel(history);
            }
        }

        // Calculate total Pages
        setTotal((int) Math.ceil((double) getRecords() / (double) getRows()));

    
           
           
           return "success";
        } catch (Exception e) {
           setMessage(e.getMessage());
           return SUCCESS;
        }
 
        
        
    }
    
    
    public String show(){
        return SUCCESS;
    }

    public String getJSON()throws Exception {
        return execute();
    }
    
    public List<History> getGridModel() {
        return gridModel;
    }

    public void setGridModel(List<History> gridModel) {
        this.gridModel = gridModel;
    }

    
    
    public List<History> getHistory() {
        return history;
    }

    public void setHistory(List<History> history) {
        this.history = history;
    }
     
     
    
    
    
    @Override
    public void setSession(Map<String, Object> map) {
         this.session = map;
    }

    public Map<String, Object> getSession() {
        return session;
    }
    
    
    
}
